/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <QScrollBar>
#include <QFileDialog>
#include <QMessageBox>
#include "imageeditor.h"
#include "imageeditarea.h"
#include "newimagedialog.h"
#include "log.h"
#include "toolpencil.h"
#include "toolselection.h"
#include "toolfill.h"

ImageEditor::ImageEditor(QWidget* parent) : QMainWindow(parent),
	m_ui(new Ui::ImageEditor)
{
	m_ui->setupUi(this);
	connect(m_ui->colorWidget, SIGNAL(foregroundColor(QColor)), m_ui->imageEditArea, SLOT(setForegroundColor(QColor)));
	m_ui->imageEditArea->setLineWidth(m_ui->sb_width->value());

	m_tools.append(EditorToolPtr(new ToolPencil()));
	m_tools.append(EditorToolPtr(new ToolSelection()));
	m_tools.append(EditorToolPtr(new ToolFill(this)));
	selectTool("pencil");
}

ImageEditor::~ImageEditor()
{
}

void ImageEditor::selectTool(int index)
{
	EditorToolPtr t = m_tools.at(index);
	t->setWidget(m_ui->imageEditArea);
	t->setImage(m_ui->imageEditArea->imagePtr());
	m_currentTool = t;
	m_ui->imageEditArea->setTool(t);
	t->mouseRelease(QPoint(0, 0));
}

void ImageEditor::selectTool(const QString& id)
{
	int index = 0;
	foreach(EditorToolPtr tool, m_tools)
	{
		if(tool->id() == id)
		{
			selectTool(index);
			return;
		}
		index++;
	}
	log(llDebug, "Tool not found: [%s]", qPrintable(id));
}

void ImageEditor::setImage(const QImage& image)
{
	m_ui->imageEditArea->setImage(image);
	m_currentTool->setImage(m_ui->imageEditArea->imagePtr());
}

QImage ImageEditor::image()
{
	return m_ui->imageEditArea->image();
}

void ImageEditor::resizeEvent(QResizeEvent* event)
{
	int hvalue = m_ui->scrollArea->horizontalScrollBar()->value();
	int vvalue = m_ui->scrollArea->verticalScrollBar()->value();
	QPoint topLeft = m_ui->scrollArea->viewport()->rect().topLeft();

	m_ui->imageEditArea->move(topLeft.x() - hvalue, topLeft.y() - vvalue);
	m_ui->imageEditArea->updateSize();
}

void ImageEditor::lineWidthChanged(int w)
{
	QPen pen = m_currentTool->pen();
	pen.setWidth(w);
	m_currentTool->setPen(pen);
}

void ImageEditor::selectPencil()
{
	selectTool("pencil");
}

void ImageEditor::selectSelection()
{
	selectTool("selection");
}

void ImageEditor::selectFill()
{
	selectTool("fill");
}

void ImageEditor::newImage()
{
	NewImageDialog dlg(this);
	if(dlg.exec() == QDialog::Accepted)
	{
		QImage newImage(dlg.imageWidth(), dlg.imageHeight(), QImage::Format_RGB32);
		newImage.fill(0xffffffff);
		setImage(newImage);
		m_currentTool->setImage(m_ui->imageEditArea->imagePtr());
	}
}

void ImageEditor::saveAsFile()
{
	QString filename = QFileDialog::getSaveFileName(this, QString(), QString(), tr("PNG Image (*.png))"));
	if(!filename.isNull())
	{
		QImage image = m_ui->imageEditArea->image();
		if(!filename.endsWith(".png"))
			filename += ".png";
		image.save(filename);
	}
}

void ImageEditor::use()
{
	QSharedPointer<QTemporaryFile> file = QSharedPointer<QTemporaryFile>(new QTemporaryFile("XXXXXX.jpg"));
	file->open();
	QImage image = m_ui->imageEditArea->image();
	image.save(file.data(), "JPG");
	log(llDebug, "Saving tmp image: %s", qPrintable(file->fileName()));
	emit imageReady(file);
	hide();
}

void ImageEditor::openFile()
{
	QString filename = QFileDialog::getOpenFileName(this, QString(), QString(), tr("PNG Image (*.png);;JPEG Image (*.jpg);;All files (*.*)"));
	if(!filename.isNull())
	{
		QImage image;
		if(!image.load(filename))
		{
			QMessageBox::critical(this, tr("Error"), tr("Unknown file format"));
			return;
		}
		m_ui->imageEditArea->setImage(image);
	}
}

